package com.atguigu.yygh.hosp.controller.api;


import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.hosp.service.DepartmentService;
import com.atguigu.yygh.hosp.service.HospitalService;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.hosp.service.ScheduleService;
import com.atguigu.yygh.hosp.utils.HttpRequestHelper;
import com.atguigu.yygh.model.hosp.Department;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.Schedule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * Created with IntelliJ IDEA
 *
 * @Date: 2022/12/19 - 14:15
 * @Author: Hyoung
 * @Description:
 */
@RestController
@Api(tags = "医院管理API接口")
@RequestMapping("/api/hosp")
@Slf4j
public class ApiController {

    @Autowired
    private HospitalService hospitalService;


    @Autowired
    private HospitalSetService hospitalSetService;

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private ScheduleService scheduleService;


    @PostMapping("/saveHospital")
    @ApiOperation(value = "保存医院信息")
    public Result<Object> saveHospital(HttpServletRequest request) {

        Map<String, Object> stringObjectMap = HttpRequestHelper.switchMap(request.getParameterMap());
        // 传输过程中base64字符串中的+被转换成了空格，需要将空格换回+
        String logoData = (String) stringObjectMap.get("logoData");
        logoData = logoData.replaceAll(" ", "+");
        stringObjectMap.put("logoData", logoData);
        // 签名校对
        String hoscode = (String) stringObjectMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);

        HttpRequestHelper.checkSign(stringObjectMap, signKey);
        // 处理业务逻辑(保存医院信息到mongodb数据库中)
        hospitalService.save(stringObjectMap);
        log.info("执行了方法saveHospital");

        return Result.ok();
    }

    @ApiOperation(value = "获取医院信息")
    @PostMapping("/hospital/show")
    public Result<Object> getHospital(HttpServletRequest request) {
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("getHospital-验签成功");
        Hospital hospital = hospitalService.getByHoscode(hoscode);
        return Result.ok(hospital);
    }

    @ApiOperation(value = "保存科室信息")
    @PostMapping("/saveDepartment")
    public Result<Object> saveDepartment(HttpServletRequest request) {
        // 签名验证
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("saveDepartment-验签成功");

        departmentService.save(paramMap);

        return Result.ok();
    }

    @ApiOperation(value = "获取分页列表")
    @PostMapping("/department/list")
    public Result<Object> department(HttpServletRequest request) {
        // 签名验证
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("department-验签成功");

        // 处理业务逻辑从mongodb数据的科室集合中获取集合信息
        // 防止医院端不穿入参数
        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        Page<Department> pageModel = departmentService.selectPage(page, limit, hoscode);
        return Result.ok(pageModel);
    }


    @ApiOperation(value = "删除一个科室信息")
    @DeleteMapping("/department/remove")
    public Result<Object> removeDepartment(HttpServletRequest request) {
        // 签名验证
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("removeDepartment-验签成功");

        String depcode = (String) paramMap.get("depcode");
        departmentService.remove(hoscode, depcode);
        return Result.ok();
    }


    @ApiOperation(value = "上传排班")
    @PostMapping("/saveSchedule")
    public Result<Object> saveSchedule(HttpServletRequest request) {
        // 签名验证
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("saveSchedule-验签成功");

        scheduleService.save(paramMap);

        return Result.ok();
    }

    @ApiOperation(value = "查询排班分页列表")
    @PostMapping("/schedule/list")
    public Result<Object> schedule(HttpServletRequest request) {
        // 签名验证
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("schedule-验签成功");

        int page = StringUtils.isEmpty(paramMap.get("page")) ? 1 : Integer.parseInt((String) paramMap.get("page"));
        int limit = StringUtils.isEmpty(paramMap.get("limit")) ? 10 : Integer.parseInt((String) paramMap.get("limit"));

        Page<Schedule> pageModel = scheduleService.selectPage(page, limit, hoscode);
        return Result.ok(pageModel);
    }

    @ApiOperation(value = "删除排班")
    @PostMapping("/schedule/remove")
    public Result<Object> removeSchedule(HttpServletRequest request) {
        // 签名验证
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(request.getParameterMap());
        String hoscode = (String) paramMap.get("hoscode");
        String signKey = hospitalSetService.getSignKey(hoscode);
        //验签
        HttpRequestHelper.checkSign(paramMap, signKey);
        log.info("removeSchedule-验签成功");
        String hosScheduleId = (String) paramMap.get("hosScheduleId");
        scheduleService.remove(hoscode, hosScheduleId);
        return Result.ok();
    }
}
